/*
 * graph2D
 * Copyright (c) 2009 Shun Moriya <shun@mnu.sakura.ne.jp>
 *
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#import "../../graphicDevice.h"
#import <UIKit/UIKit.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/EAGLDrawable.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
#import <QuartzCore/QuartzCore.h>

#define GRAPH2D_DESKTOP_VIEW_USE_DISPLAY_LINK

@interface DesktopView : UIView
{
	EAGLContext* context;

	CGSize screenSize;

	GLuint viewRenderbuffer;
	GLuint viewFramebuffer;
	GLuint depthRenderbuffer;

#if defined(GRAPH2D_DESKTOP_VIEW_USE_DISPLAY_LINK)
	CADisplayLink* animationTimer;
	NSInteger animationInterval;
	float m_timestamp;
#else
	NSTimer* animationTimer;
	NSTimeInterval animationInterval;
#endif
	bool animationRunning;
}
@property (nonatomic, retain) EAGLContext* context;
#if defined(GRAPH2D_DESKTOP_VIEW_USE_DISPLAY_LINK)
@property (nonatomic, assign) CADisplayLink* animationTimer;
@property (readonly) NSInteger animationInterval;
#else
@property (nonatomic, assign) NSTimer* animationTimer;
@property (readonly) NSTimeInterval animationInterval;
#endif

-(id)initWithFrame:(CGRect)aRect scale:(float)contentScaleFactor screen:(CGSize)screen;
-(void)dealloc;
@end

@interface DesktopView(Singleton)
+(DesktopView*)setup:(CGRect)aRect scale:(float)contentScaleFactor buffer:(void*)buffer size:(size_t)size;
-(void)finalize;
+(DesktopView*)instance;
+(id)allocWithZone:(NSZone*)zone;
-(id)copyWithZone:(NSZone*)zone;
-(id)retain;
-(unsigned)returnCount;
-(void)release;
-(id)autorelease;
@end

@interface DesktopView(Attach)
-(void)attach;
-(void)dettach;
@end

@interface DesktopView(Animation)
-(void)initializeAnimation;
-(void)finalizeAnimation;
#if defined(GRAPH2D_DESKTOP_VIEW_USE_DISPLAY_LINK)
-(void)setAnimationInterval:(NSInteger)interval;
#else
-(void)setAnimationTimer:(NSTimer*)newTimer;
-(void)setAnimationInterval:(NSTimeInterval)interval;
#endif
-(void)startAnimation;
-(void)stopAnimation;
@end

@interface DesktopView(Mouse)
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event;
-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event;
-(void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event;
@end
